package lin.an.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lin.an.food.entity.FoodReview;
import lin.an.food.mapper.FoodReviewMapper;
import lin.an.food.service.FoodReviewService;
import lin.an.user.entity.User;
import lin.an.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhoulinq
 * @date 2025/4/12
 */
@Service
public class FoodReviewServiceImpl extends ServiceImpl<FoodReviewMapper, FoodReview> implements FoodReviewService {
    @Autowired
    private UserService userService;
    @Override
    public List<FoodReview> getReviewsByFoodId(int foodId) {
        LambdaQueryWrapper<FoodReview> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FoodReview::getFoodId, foodId)
                .orderByDesc(FoodReview::getCreateTime);
        return this.list(wrapper);
    }

    @Override
    public boolean addReview(FoodReview review) {
        User user = userService.getById(review.getUserId());
        review.setUserAvatar(user.getAvatar());
        review.setUserName(user.getUserName());
        return this.save(review);
    }

    @Override
    public boolean deleteReview(int reviewId, int userId) {
        LambdaQueryWrapper<FoodReview> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FoodReview::getReviewId, reviewId)
                .eq(FoodReview::getUserId, userId);
        return this.remove(wrapper);
    }
}
